﻿using System;
using System.Linq;
using LINQPad.Extensibility.DataContext;
using LINQPad.OpenAccess.Settings;

namespace LINQPad.OpenAccess.Services.Descriptors
{
    /// <summary>
    ///   <see cref="IConnectionDescriptor" /> for using the default constructor.
    /// </summary>
    public class ConnectionDescriptorForUsingDefaultConstructor : ConnectionDescriptorBase
    {
        #region Public

        #region - Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionDescriptorForUsingDefaultConstructor" /> class.
        /// </summary>
        public ConnectionDescriptorForUsingDefaultConstructor( )
            : base( Int32.MaxValue ) { }

        #endregion

        #endregion

        #region IConnectionDescriptor Members

        /// <summary>
        /// Determines whether this instance is able to describe the specified connection.
        /// </summary>
        /// <param name="connectionInfo">The connection info.</param>
        /// <param name="driverSettings">The driver settings.</param>
        /// <returns>
        ///   <c>true</c> if this instance is able to describe the specified connection; otherwise, <c>false</c>.
        /// </returns>
        public override bool CanDescribe( IConnectionInfo connectionInfo, DriverSettings driverSettings )
        {
            return connectionInfo != null
                    && driverSettings != null
                    && driverSettings.Initialization != null
                    && driverSettings.Initialization.UseDefaultConstructor;
        }
        /// <summary>
        /// Describes the specified connection.
        /// </summary>
        /// <param name="connectionInfo">The connection info.</param>
        /// <param name="driverSettings">The driver settings.</param>
        /// <returns>
        /// The description.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override string Describe( IConnectionInfo connectionInfo, DriverSettings driverSettings )
        {
            var contextTypeName = this.GetContextTypeName( connectionInfo );
            var assemblyPath = this.GetContextAssemblyPath( connectionInfo );
            if ( String.IsNullOrWhiteSpace( assemblyPath ) )
                return contextTypeName;
            return String.Format( "{0} in {1}", contextTypeName, assemblyPath );
        }

        #endregion
    }
}
